package com.fenghongzhang.day002;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private TextView text1;

    private long time = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //是不是back
        if(keyCode ==  KeyEvent.KEYCODE_BACK){ //
            if(System.currentTimeMillis() - time > 2000){ // 100000000  - 0 > 2000  2.100000002 -100000001 < 2000
                Toast.makeText(this, "再按就推出", Toast.LENGTH_SHORT).show();
                //截断点击事件, 不在继续向下传递.
                time = System.currentTimeMillis(); // 100000001
                return true;
            }else{
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text);
        text1 = (TextView) findViewById(R.id.text1);

        System.currentTimeMillis();

        //注册
        registerForContextMenu(text);

        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1,上下文,2要实现在哪个控件的下面
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, text);
                //2加载布局
                popupMenu.inflate(R.menu.menu);

                //3点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //点击事件
                        int itemId = item.getItemId();
                        switch (itemId){
                            case R.id.red:
                                Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.blue:
                                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.yellow:
                                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                                break;
                        }
                        return false;
                    }
                });

                //4
                popupMenu.show();

            }
        });

    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //加载自定义的菜单布局
        getMenuInflater().inflate(R.menu.menu,menu);

//        menu.addSubMenu()

    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //点击事件
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.yellow:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.er:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //加载自定义的菜单布局
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //点击事件
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.yellow:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void btn(View view) {
        long l = System.currentTimeMillis();
        //1970的1,1 现在一个多少的时间,毫秒值
        Toast.makeText(this, ""+l, Toast.LENGTH_SHORT).show();

        final PopupWindow popupWindow = new PopupWindow(this);

        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);

        //内容的点击事件
        TextView textView = inflate.findViewById(R.id.pop_tv1);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });



        //设置窗口背景变暗
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getWindow().setAttributes(attributes);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;
                getWindow().setAttributes(attributes);
            }
        });


        //设置一个动画样式.
        popupWindow.setAnimationStyle(R.style.pop);

        //三个要求
        popupWindow.setWidth(300);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(inflate);

        //点击外部窗口可以消失
        popupWindow.setOutsideTouchable(true);

        //显示的位置
//        popupWindow.showAsDropDown(text,200,200);
        View inflate1 = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);

    }
}
